package com.future.bantang.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.future.bantang.R;
import com.future.bantang.adapter.FraGoodthingsGridAdapter;
import com.future.bantang.constants.BanTang;
import com.future.bantang.constants.URL;
import com.future.bantang.entity.GoodThingsProductEntity;
import com.future.bantang.utils.JsonUtils;
import com.future.bantang.utils.L;
import com.future.bantang.utils.NetUtils;
import com.future.bantang.view.TitleBarView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

/**
 * 好物 商品列表界面
 */
public class FragmentGoodthings extends Fragment {

    public static final String TAG="FragmentGoodthings";
//    @ViewInject(R.id.grid_fra_goodthings_Productlist)
    PullToRefreshGridView productGrid;

    private String url;
    private String categoryId;
    private int page = 0;
    private int pagesize = 20;
    private FraGoodthingsGridAdapter gridAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.url = URL.PRODUCT_LIST_URL;
        this.categoryId=getArguments().getString("categoryId");
    }

    /**
     * 外界传递的分类id决定实际的fragment对象
     *
     * @param categoryId
     * @return
     */
    public static FragmentGoodthings newInstance(String categoryId) {
        FragmentGoodthings f=new FragmentGoodthings();
        Bundle b=new Bundle();
        b.putString("categoryId", categoryId);
        f.setArguments(b);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_goodthings, container, false);


    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        productGrid= (PullToRefreshGridView) view.findViewById(R.id.grid_fra_goodthings_Productlist);
        productGrid.setMode(PullToRefreshBase.Mode.BOTH);
        initView(view);
        loadDatas();
    }

    private void initView(View view) {
        gridAdapter = new FraGoodthingsGridAdapter(getActivity());
        productGrid.setAdapter(gridAdapter);
        productGrid.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                page=0;
                loadDatas();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                loadDatas();
            }
        });

    }


    private void loadDatas() {
        NetUtils.RequestParamsBuilder builder=new NetUtils.RequestParamsBuilder();

        RequestParams params = builder.addPostParameter(BanTang.CLIENT_ID_KEY,BanTang.CLIENT_ID_VALUE)
                .addPostParameter(BanTang.CLIENT_SECRET_KEY, BanTang.CLIENT_SECRET_VALUE)
                .addPostParameter("category",categoryId)
                .addPostParameter(BanTang.PAGE, page + "")
                .addPostParameter(BanTang.PAGESIZE, pagesize + "")
                .build();

        NetUtils.doPost(url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    GoodThingsProductEntity data = JsonUtils.perse(responseInfo.result, GoodThingsProductEntity.class);
                    List<GoodThingsProductEntity.DataEntity.ProductEntity> product = data.getData().getProduct();
                    if (product != null) {

                        if (page > 0) {   //
                            gridAdapter.addDatas(product);
                        } else { //初次加载

                            gridAdapter.setDatas(product);
                        }
                        //页数加1
                        page++;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }


                productGrid.onRefreshComplete();
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Toast.makeText(getActivity(), "获取数据失败，请检查网络！", Toast.LENGTH_SHORT).show();
                productGrid.onRefreshComplete();
            }
        });



    }
}
